#ifndef __RT_DEF_H__
#define __RT_DEF_H__

#include <rtconfig.h>

/* RT-Thread basic data type definitions */
typedef signed   char                   rt_int8_t;      /**<  8bit integer type */
typedef signed   short                  rt_int16_t;     /**< 16bit integer type */
typedef signed   long                   rt_int32_t;     /**< 32bit integer type */
typedef unsigned char                   rt_uint8_t;     /**<  8bit unsigned integer type */
typedef unsigned short                  rt_uint16_t;    /**< 16bit unsigned integer type */
typedef unsigned long                   rt_uint32_t;    /**< 32bit unsigned integer type */
typedef int                             rt_bool_t;      /**< boolean type */

/* 32bit CPU */
typedef long                            rt_base_t;      /**< Nbit CPU related date type */
typedef unsigned long                   rt_ubase_t;     /**< Nbit unsigned CPU related data type */

typedef rt_base_t                       rt_err_t;       /**< Type for error number */
typedef rt_uint32_t                     rt_time_t;      /**< Type for time stamp */
typedef rt_uint32_t                     rt_tick_t;      /**< Type for tick count */
typedef rt_base_t                       rt_flag_t;      /**< Type for flags */
typedef rt_ubase_t                      rt_size_t;      /**< Type for size number */
typedef rt_ubase_t                      rt_dev_t;       /**< Type for device */
typedef rt_base_t                       rt_off_t;       /**< Type for offset */

/* boolean type definitions */
#define RT_TRUE                         1               /**< boolean true  */
#define RT_FALSE                        0               /**< boolean fails */

/* Compiler Related Definitions */
#ifdef __CC_ARM                         /* ARM Compiler */

#define rt_inline                   static __inline
#define ALIGN(n)                    __attribute__((aligned(n)))

#elif defined (__IAR_SYSTEMS_ICC__)     /* for IAR Compiler */

#define rt_inline                   static inline
#define ALIGN(n)                    PRAGMA(data_alignment=n)

#elif defined (__GNUC__)                /* GNU GCC Compiler */

#define rt_inline                   static __inline
#define ALIGN(n)                    __attribute__((aligned(n)))

#else
    #error not supported tool chain
#endif

/**
 * @ingroup BasicDef
 *
 * @def RT_ALIGN(size, align)
 * Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
 * would return 16.
 */
#define RT_ALIGN(size, align)           (((size) + (align) - 1) & ~((align) - 1))

/**
 * @ingroup BasicDef
 *
 * @def RT_ALIGN_DOWN(size, align)
 * Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
 * would return 12.
 */
#define RT_ALIGN_DOWN(size, align)      ((size) & ~((align) - 1))


/* RT-Thread error code definitions */
#define RT_EOK                          0               /**< There is no error */
#define RT_ERROR                        1               /**< A generic error happens */
#define RT_ETIMEOUT                     2               /**< Timed out */
#define RT_EFULL                        3               /**< The resource is full */
#define RT_EEMPTY                       4               /**< The resource is empty */
#define RT_ENOMEM                       5               /**< No memory */
#define RT_ENOSYS                       6               /**< No system */
#define RT_EBUSY                        7               /**< Busy */
#define RT_EIO                          8               /**< IO error */
#define RT_EINTR                        9               /**< Interrupted system call */
#define RT_EINVAL                       10              /**< Invalid argument */

/* maximum value of base type */
#define RT_UINT8_MAX                    0xff            /**< Maxium number of UINT8 */
#define RT_UINT16_MAX                   0xffff          /**< Maxium number of UINT16 */
#define RT_UINT32_MAX                   0xffffffff      /**< Maxium number of UINT32 */
#define RT_TICK_MAX                     RT_UINT32_MAX   /**< Maxium number of tick */

/**
 * @ingroup BasicDef
 *
 * @def RT_NULL
 * Similar as the \c NULL in C library.
 */
#define RT_NULL                         (0)

/**
 * Double List structure
 */
struct rt_list_node
{
    struct rt_list_node *next;                          /**< point to next node. */
    struct rt_list_node *prev;                          /**< point to prev node. */
};
typedef struct rt_list_node rt_list_t;                  /**< Type for lists. */

/**
 * Single List structure
 */
struct rt_slist_node
{
    struct rt_slist_node *next;                         /**< point to next node. */
};
typedef struct rt_slist_node rt_slist_t;                /**< Type for single list. */

/**
 * Base structure of Kernel object
 */
struct rt_object
{
    char       name[RT_NAME_MAX];                       /**< name of kernel object 内核对象的名字 */
    rt_uint8_t type;                                    /**< type of kernel object 内核对象的类型 */
    rt_uint8_t flag;                                    /**< flag of kernel object 内核对象的状态 */

    rt_list_t  list;                                    /**< list node of kernel object 内核对象的列表节点 */
};
typedef struct rt_object *rt_object_t;                  /**< Type for kernel objects. 内核对象数据类型重定义 */

/**
 *  The object type can be one of the follows with specific
 *  macros enabled:
 *  - Thread
 *  - Semaphore
 *  - Mutex
 *  - Event
 *  - MailBox
 *  - MessageQueue
 *  - MemHeap
 *  - MemPool
 *  - Device
 *  - Timer
 *  - Module
 *  - Unknown
 *  - Static
 */
enum rt_object_class_type
{
		RT_Object_Class_Thread = 0, /* 对象是线程 */
		RT_Object_Class_Semaphore, /* 对象是信号量 */
		RT_Object_Class_Mutex, /* 对象是互斥量 */
		RT_Object_Class_Event, /* 对象是事件 */
		RT_Object_Class_MailBox, /* 对象是邮箱 */
		RT_Object_Class_MessageQueue, /* 对象是消息队列 */
		RT_Object_Class_MemHeap, /* 对象是内存堆 */
		RT_Object_Class_MemPool, /* 对象是内存池 */
		RT_Object_Class_Device, /* 对象是设备 */
		RT_Object_Class_Timer, /* 对象是定时器 */
		RT_Object_Class_Module, /* 对象是模块 */
		RT_Object_Class_Unknown, /* 对象未知 */
		RT_Object_Class_Static = 0x80 /* 对象是静态对象 */
};

/**
 * The information of the kernel object
 */
struct rt_object_information
{
    enum rt_object_class_type type;                     /**< object class type 对象类型 */
    rt_list_t                 object_list;              /**< object list 对象列表节点头 */
    rt_size_t                 object_size;              /**< object size 对象大小  */
};


/**
 * @addtogroup Clock
 */

/*@{*/

/**
 * clock & timer macros
 */
#define RT_TIMER_FLAG_DEACTIVATED       0x0             /**< timer is deactive 定时器没有激活 */
#define RT_TIMER_FLAG_ACTIVATED         0x1             /**< timer is active 定时器已经激活 */
#define RT_TIMER_FLAG_ONE_SHOT          0x0             /**< one shot timer 单次定时 */
#define RT_TIMER_FLAG_PERIODIC          0x2             /**< periodic timer 周期定时 */

#define RT_TIMER_FLAG_HARD_TIMER        0x0             /**< hard timer,the timer's callback function will be called in tick isr. */
#define RT_TIMER_FLAG_SOFT_TIMER        0x4             /**< soft timer,the timer's callback function will be called in timer thread. */

#define RT_TIMER_CTRL_SET_TIME          0x0             /**< set timer control command */
#define RT_TIMER_CTRL_GET_TIME          0x1             /**< get timer control command */
#define RT_TIMER_CTRL_SET_ONESHOT       0x2             /**< change timer to one shot */
#define RT_TIMER_CTRL_SET_PERIODIC      0x3             /**< change timer to periodic */

#ifndef RT_TIMER_SKIP_LIST_LEVEL
#define RT_TIMER_SKIP_LIST_LEVEL          1
#endif

/* 1 or 3 */
#ifndef RT_TIMER_SKIP_LIST_MASK
#define RT_TIMER_SKIP_LIST_MASK         0x3
#endif


/**
 * timer structure
 */
struct rt_timer
{
    struct rt_object parent;                            /**< inherit from rt_object 从 rt_object 继承  */

		rt_list_t        row[RT_TIMER_SKIP_LIST_LEVEL];			/* 节点，用于将timer挂到不同的list链表上 */

    void (*timeout_func)(void *parameter);              /**< timeout function 超时回调函数 */
    void            *parameter;                         /**< timeout function's parameter 参数 */

    rt_tick_t        init_tick;                         /**< timer timeout tick 定时器实际需要延时的时间 */
    rt_tick_t        timeout_tick;                      /**< timeout tick 定时器实际超时时的系统节拍数  */
};
typedef struct rt_timer *rt_timer_t;


/*
* 线程状态定义
*/
#define RT_THREAD_INIT 						0x00 /* 初始态 */
#define RT_THREAD_READY 					0x01 /* 就绪态 */
#define RT_THREAD_SUSPEND 				0x02 /* 挂起态 */
#define RT_THREAD_RUNNING 				0x03 /* 运行态 */
#define RT_THREAD_BLOCK 					RT_THREAD_SUSPEND /* 阻塞态 */
#define RT_THREAD_CLOSE 					0x04 /* 关闭态 */
#define RT_THREAD_STAT_MASK 			0x0f

#define RT_THREAD_STAT_SIGNAL 		0x10
#define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY)
#define RT_THREAD_STAT_SIGNAL_SUSPEND 	0x20
#define RT_THREAD_STAT_SIGNAL_MASK 			0xf0

/**
 * Thread structure
 */
struct rt_thread
{
	  /* rt object */
    char        name[RT_NAME_MAX];                      /**< the name of thread 对象的名字 */
    rt_uint8_t  type;                                   /**< type of object 对象类型 */
    rt_uint8_t  flags;                                  /**< thread's flags 对象的状态 */

    rt_list_t   list;                                   /**< the object list 对象的列表节点 */
    rt_list_t   tlist;                                  /**< the thread list 线程链表节点 */
		
    /* stack point and entry */
    void       *sp;                                     /**< stack point */
    void       *entry;                                  /**< entry */
    void       *parameter;                              /**< parameter */
    void       *stack_addr;                             /**< stack address */
    rt_uint32_t stack_size;                             /**< stack size */
	
		rt_ubase_t init_tick; 															/* 初始时间片 */
		rt_ubase_t remaining_tick; 													/* 剩余时间片 */
		
		rt_uint8_t current_priority; 												/* 当前优先级 */
		rt_uint8_t init_priority; 													/* 初始优先级 */
		rt_uint32_t number_mask; 														/* 当前优先级掩码 */
		
		rt_err_t error; 																		/* 错误码 */
		rt_uint8_t stat; 																		/* 线程的状态 */
		
		struct rt_timer thread_timer; 											/* 内置的线程定时器 */

};
typedef struct rt_thread *rt_thread_t;



#endif //__RT_DEF_H__
